package com.xyht.medicine_management_system.modules.collect.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xyht.medicine_management_system.common.commonReturn.CommonCodeEnum;
import com.xyht.medicine_management_system.common.commonReturn.CommonResult;
import com.xyht.medicine_management_system.common.commonReturn.ResponseResult;
import com.xyht.medicine_management_system.modules.collect.entity.MmsMedicineCollect;
import com.xyht.medicine_management_system.modules.collect.entity.req.MmsMedicineCollectAddReq;
import com.xyht.medicine_management_system.modules.collect.entity.req.MmsMedicineCollectDeletedReq;
import com.xyht.medicine_management_system.modules.collect.entity.resp.MmsMedicineCollectListResp;
import com.xyht.medicine_management_system.modules.collect.mapper.MmsMedicineCollectMapper;
import com.xyht.medicine_management_system.modules.collect.service.MmsMedicineCollectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xyht.medicine_management_system.modules.medicine.entity.MmsMedicine;
import com.xyht.medicine_management_system.modules.medicine.entity.MmsMedicineSpecial;
import com.xyht.medicine_management_system.modules.medicine.entity.resp.MmsMedicineDetailResp;
import com.xyht.medicine_management_system.modules.medicine.mapper.MmsMedicineMapper;
import com.xyht.medicine_management_system.modules.medicine.mapper.MmsMedicineSpecialMapper;
import com.xyht.medicine_management_system.modules.medicineSource.entity.MmsMedicineSource;
import com.xyht.medicine_management_system.modules.medicineSource.entity.resp.MmsMedicineSourceResp;
import com.xyht.medicine_management_system.modules.medicineSource.mapper.MmsMedicineSourceMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import java.util.List;
import java.util.stream.Collectors;

import static com.xyht.medicine_management_system.common.util.ParamCheckUtil.isNullOrEmpty;
import static com.xyht.medicine_management_system.modules.collect.constant.CollectTypeConstant.COLLECT_TYPE_MEDICINE;
import static com.xyht.medicine_management_system.modules.collect.constant.CollectTypeConstant.COLLECT_TYPE_MEDICINE_SOURCE;
import static com.xyht.medicine_management_system.modules.medicine.constant.MedicineIsSpecialConstant.MEDICINE_IS_NOT_SPECIAL;
import static com.xyht.medicine_management_system.modules.medicine.constant.MedicineIsSpecialConstant.MEDICINE_IS_SPECIAL;

/**
 * <p>
 * 药品 药资源 收藏 服务实现类
 * </p>
 *
 * @author XYHT
 * @since 2023-12-11
 */
@Service
public class MmsMedicineCollectServiceImpl extends ServiceImpl<MmsMedicineCollectMapper, MmsMedicineCollect> implements MmsMedicineCollectService {

    @Resource
    private MmsMedicineCollectMapper medicineCollectMapper;
    @Resource
    private MmsMedicineMapper medicineMapper;
    @Resource
    private MmsMedicineSourceMapper medicineSourceMapper;
    @Resource
    private MmsMedicineSpecialMapper medicineSpecialMapper;


    @Override
    public ResponseResult addCollect(MmsMedicineCollectAddReq addReq, String userId) {
        if (isNullOrEmpty(addReq.getCollectId(),addReq.getCollectType())) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        MmsMedicineCollect medicineCollect = new MmsMedicineCollect();
        String collectId = addReq.getCollectId();
        //类型 1: 药品 2:药知源
        Integer collectType = addReq.getCollectType();
        //校验是否存在
        if (collectType == COLLECT_TYPE_MEDICINE) {
            MmsMedicine medicine = medicineMapper.selectById(collectId);
            if (isNullOrEmpty(medicine)) {
                return CommonResult.failed(CommonCodeEnum.MEDICINE_NOT_EXIST);
            }
        }else if(collectType == COLLECT_TYPE_MEDICINE_SOURCE){
            MmsMedicineSource medicineSource = medicineSourceMapper.selectById(collectId);
            if (isNullOrEmpty(medicineSource)) {
                return CommonResult.failed(CommonCodeEnum.MEDICINE_SOURCE_NOT_EXIST);
            }
        }

        //校验是否重复
        List<MmsMedicineCollect> mmsMedicineCollects = medicineCollectMapper.selectList(new LambdaQueryWrapper<MmsMedicineCollect>()
                .eq(MmsMedicineCollect::getCollectId, collectId)
                .eq(MmsMedicineCollect::getUserId, userId));
        if (mmsMedicineCollects.isEmpty()) {
            medicineCollect.setCollectId(collectId);
            medicineCollect.setCollectType(collectType);
            medicineCollect.setUserId(userId);
            medicineCollectMapper.insert(medicineCollect);
        }
        String id = medicineCollect.getId();
        return CommonResult.success(id);
    }

    @Override
    public ResponseResult deletedCollect(MmsMedicineCollectDeletedReq deletedReq) {
        if (isNullOrEmpty(deletedReq.getId())) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }

        MmsMedicineCollect medicineCollect = medicineCollectMapper.selectById(deletedReq.getId());
        if (isNullOrEmpty(medicineCollect)) {
            return CommonResult.failed(CommonCodeEnum.COLLECT_NOT_EXIST);
        }

        medicineCollectMapper.deleteById(deletedReq.getId());
        return CommonResult.success();
    }

    @Override
    public ResponseResult getCollectList(Integer pageNum, Integer pageSize, String userId, Integer type) {
        if (isNullOrEmpty(type)) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        Page<MmsMedicineCollect> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<MmsMedicineCollect> medicineCollectLambdaQueryWrapper =
                new LambdaQueryWrapper<MmsMedicineCollect>()
                        .eq(MmsMedicineCollect::getUserId, userId)
                        .eq(MmsMedicineCollect::getCollectType,type)
                        .orderByDesc(MmsMedicineCollect::getCreateTime);
        medicineCollectMapper.selectPage(page, medicineCollectLambdaQueryWrapper);
        List<MmsMedicineCollectListResp> medicineCollectList = page.getRecords()
                .stream()
                .map(mmsMedicineCollect -> {
                    Integer collectType = mmsMedicineCollect.getCollectType();
                    MmsMedicineCollectListResp medicineCollectResp = new MmsMedicineCollectListResp();
                    medicineCollectResp.setId(mmsMedicineCollect.getId());
                    MmsMedicineDetailResp medicineDetailResp = new MmsMedicineDetailResp();
                    MmsMedicineSourceResp medicineSourceResp = new MmsMedicineSourceResp();
                    // 1: 药品
                    if (collectType == COLLECT_TYPE_MEDICINE) {
                        medicineCollectResp.setType(COLLECT_TYPE_MEDICINE);
                        MmsMedicine medicine = medicineMapper.selectById(mmsMedicineCollect.getCollectId());
                        if (!isNullOrEmpty(medicine)) {
                            BeanUtils.copyProperties(medicine, medicineDetailResp);
                            //判断是否特惠商品
                            List<MmsMedicineSpecial> specialList = medicineSpecialMapper.selectList(new LambdaQueryWrapper<MmsMedicineSpecial>()
                                    .eq(MmsMedicineSpecial::getMedicineId, medicine.getId()));
                            if (specialList != null && specialList.size() > 0) {
                                medicineDetailResp.setIsSpecial(MEDICINE_IS_SPECIAL);
                                medicineDetailResp.setSpecialPrice(specialList.get(0).getSpecialPrice());
                            } else {
                                medicineDetailResp.setIsSpecial(MEDICINE_IS_NOT_SPECIAL);
                            }
                        }

                        //2:药知源
                    } else if (collectType == COLLECT_TYPE_MEDICINE_SOURCE) {
                        medicineCollectResp.setType(COLLECT_TYPE_MEDICINE_SOURCE);
                        MmsMedicineSource mmsMedicineSource = medicineSourceMapper.selectById(mmsMedicineCollect.getCollectId());
                        if (!isNullOrEmpty(mmsMedicineSource)) {
                            BeanUtils.copyProperties(mmsMedicineSource, medicineSourceResp);
                        }
                    }
                    medicineCollectResp.setMedicineInfo(medicineDetailResp);
                    medicineCollectResp.setMedicineSourceInfo(medicineSourceResp);
                    return medicineCollectResp;
                })
                .collect(Collectors.toList());
        int total = (int) page.getTotal();
        return CommonResult.success(medicineCollectList, total);
    }
}
